package com.Brave.manager.dao;

import com.Brave.manager.daomian.Student;

public class StudentDao {
    private static Student[] Students = new Student[8];
    static {
        Student stu1 = new Student("001","胖鸡","18","男");
        Student stu2 = new Student("002","晨曦","20","女");
        Students[0] = stu1;
        Students[1] = stu2;
    }
    public Student[] findAllStudent() {
        return  Students;
    }

    public boolean addStudnet(Student stu) {
        int index = -1;
        for (int i = 0; i < Students.length; i++) {
            Student Stu = Students[i];
            if(Stu == null){
                index = i;
                break;
            }
        }
        if(index == -1){
            return false;
        }else {
            Students[index] = stu;
            return true;
        }
    }

    public void deleteStudent(String id) {
        int index = getIndex(id);
        Students[index] = null;
    }

    private int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < Students.length; i++) {
            Student stu = Students[i];
            if(stu != null && stu.getId().equals(id)){
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateStudent(String id, Student stu) {
        int index = getIndex(id);
        Students[index] = stu;
    }
}
